package com.bt.lms.ui.action.school_education.stu_msg_manage;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bt.lms.constants.BOConstant;
import com.bt.lms.constants.StuStatusBizTypeConstant;
import com.bt.lms.entity.FamilyMemberInfo;
import com.bt.lms.entity.StuBasicInfo;
import com.bt.lms.network.msg.ReqMsg;
import com.bt.lms.network.msg.RespMsg;
import com.bt.lms.network.msg.packet.ReqMsgPacket;
import com.bt.lms.ui.page.common.action.pagerecord.RecruitStuLabAction;
import com.bt.lms.ui.page.school_education.query_details.PageCheckManage;
import com.bt.lms.ui.vo.school_education.family_member.AddFamilyMemberVo;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Cursor;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import org.apache.commons.beanutils.BeanUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.Socket;

/**
 * 查看学员详情功能事件
 */
public class CheckDetailsAction implements EventHandler {

    private Label label;

    public CheckDetailsAction(Label name1Lab) {
        this.label = name1Lab;
    }

    @Override
    public void handle(Event event) {

        //点击进入学员详情
        label.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                StuBasicInfo stuBasicInfo = new StuBasicInfo();
                //组包
                byte[] bytes = enCode(stuBasicInfo);
                //组网
                Socket socket = getNet();
                //发送消息
                boolean flag = sendMsg(socket, bytes);


                PageCheckManage pageCheckManage = new PageCheckManage();
                Pane pane = RecruitStuLabAction.pageSkipInfo.getPane();
                ObservableList<Node> children = pane.getChildren();
                Integer index = RecruitStuLabAction.pageSkipInfo.getIndex();
                children.set(index,pageCheckManage);
            }

            /**
             * 发送消息
             * @param socket 网
             * @param bytes 消息包
             * @return 是否成功
             */
            private boolean sendMsg(Socket socket, byte[] bytes) {
//
                try {
                    OutputStream out = socket.getOutputStream();
                    out.write(bytes);
                    out.flush();


                    InputStream in = socket.getInputStream();
                    byte[] buf = new byte[10000];
                    in.read(buf);
                    RespMsg respMsg = RespMsg.toRespMsg(buf);
                    String data = respMsg.getData();
                    FamilyMemberInfo familyMemberInfo = JSON.parseObject(data, FamilyMemberInfo.class);
                    AddFamilyMemberVo addFamilyMemberVo = new AddFamilyMemberVo();
                    BeanUtils.copyProperties(addFamilyMemberVo,familyMemberInfo);


                    //关闭资源
                    out.close();
                    socket.close();

                } catch (IOException | InvocationTargetException | IllegalAccessException e) {
                    throw new RuntimeException(e);
                }

                return true;

            }

            /**
             * 组网
             * @return 网
             */
            private Socket getNet() {
                Socket socket = null;
                try {
                    socket = new Socket("127.0.0.1",8888);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

                    return socket;
            }

            /**
             * 组包
             * @param stuBasicInfo 学员信息
             * @return 消息包
             */
            private byte[] enCode(StuBasicInfo stuBasicInfo) {
                ReqMsg reqMsg = new ReqMsg();
                reqMsg.setBizType(StuStatusBizTypeConstant.QUERY_DETAILS_BIZ_TYPE);
                stuBasicInfo.setId(1L);
                String details = JSON.toJSONString(stuBasicInfo);

                reqMsg.setData(details);
                byte[] bytes = reqMsg.toBytes();



                return bytes;
            }
        });

        //鼠标移动到label位置变颜色，变成小手图案
        label.addEventHandler(MouseEvent.MOUSE_ENTERED_TARGET, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                label.setStyle("-fx-text-fill: #1bde2c");//变成红颜色
                label.setCursor(Cursor.HAND);//变成小手图案
            }
        });
        //鼠标退出时变回原来颜色
        label.addEventHandler(MouseEvent.MOUSE_EXITED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                label.setStyle("-fx-text-fill: #00ccff");
            }
        });
    }
}
